Skip to content

fix(search-events): Keep the agent's absolute window when a period is also passed - #2146

Merged
joshuarli merged 1 commit into
mainfrom
fix/search-events-narrow-period-to-agent-range
Nov 15, 2026
Merged

joshuarli merged 1 commit into
mainfrom
fix/search-events-narrow-period-to-agent-range

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

search_errors (and the other runSearchEvents tools) can silently search the whole period when the query names a fixed window. This happens when the caller names a window in the query text, uses some structured filters, and also sends period as an outer guard.

What happens

  1. The query has structured filters, so hasStructuredQuery is true. Dataset tools set lockDataset, so the handler trusts the caller's explicit params.
  2. The embedded agent sees statsPeriod in its prompt. It still returns an absolute timeRange ({start, end}) for the window that the query names.
  3. Because period is explicit, the handler discards that range and sends only statsPeriod (search.ts:936-938 on main). executedSearch.timeRange shows statsPeriod with null start/end. The explanation still describes the agent's plan.

A production trace from user feedback (MCP-SERVER-GN4) shows exactly this. The query was Between <start> and <end>, environment:production <tag>:<value>, … with period: "3d". The agent's first events/validate call used start/end. The final /events/ request and the Snuba query used the 3-day rolling window. The same session without structured filters took the untrusted path, and the absolute range was applied correctly.

Change

When the caller passes period and the agent returns an absolute range, the search uses the overlap of the two. The overlap can only narrow period, so the result never goes outside what the caller allowed. When the two don't overlap, or the agent returned a relative period, the caller's period is kept as before.

Relation to other work

via Sentry.

--

View Session [Sentry]

… also passed

For structured queries with an explicit `period`, the handler dropped the
embedded agent's absolute `start`/`end` and searched the whole period. Callers
that name a fixed window in the query and send `period` as an outer guard got
rolling-period results without being told.

Use the overlap of the agent's absolute range and the period instead. It can
only narrow the period; with no overlap, the period is kept.
@vercel

vercel Bot commented Nov 10, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Nov 10, 2026 4:44pm UTC

Request Review

@sentry-junior

sentry-junior Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Another production case for this path, from Sentry MCP feedback MCP-SERVER-GNC (claude.ai, search_traces, server 0.42.0).

Span 983d66c5 in trace 94f5d6d4 (2026-11-10 21:31): period: "9d", query span.op:db timestamp:>=2026-11-02T13:50:00 timestamp:<2026-11-02T14:00:00. The agent's first events/validate used start/end, and its explanation says it "replaced the conflicting 9-day relative range with the explicit 10-minute absolute range". The final /events/ request still sent statsPeriod=9d, and executedSearch.timeRange was {statsPeriod: "9d", start: null, end: null}. The timestamp: filters stayed in the query, so this aggregate was still scoped. A time series, though, gets buckets over the whole period, and only the newest 48 come back (#2153 adds a note for that). This change would have run the 10-minute window.

What this PR doesn't cover: the feedback's earlier calls were natural language with no structured filters, for example count of db spans per 5 minutes, where span.op is db and timestamp between 2026-11-02T13:30:00Z and 2026-11-02T16:30:00Z. looksLikeSentrySearchSyntax is false for all three phrasings, so they take the untrusted path, where the handler uses the agent's timeRange as it is (search.ts:935-938). The caller still saw statsPeriod: "9d". So either the agent returned the caller's period, which the prompt lists as a current parameter (search.ts:399-407), or the provider fallback ran (timeRange: { statsPeriod: params.period }, search.ts:869). Those calls weren't sampled, so I can't tell which. Typed start/end (#1215) would remove the guesswork either way.

joshuarli pushed a commit that referenced this pull request Nov 14, 2026
`search_issue_events` fails on open-ended "since X" windows such as
`timestamp:>=2026-11-12T06:39:00Z` or "since November 12 2026 at 06:39
UTC". It returns "The AI was unable to process your query";
`search_errors` handles the same window fine.

**Why it fails**

The embedded agent's output schema
(`tools/support/search-issue-events/agent.ts`) is stricter than its
prompt:

1. An absolute `timeRange` requires both `start` and `end`. A "since X"
request has no natural end, and the agent doesn't know the current time.
2. `timeRange` is required, but the prompt says to omit it for the
default window.

If the model returns `{start}` only, or omits `timeRange`, the AI SDK
raises `NoObjectGeneratedError` ("response did not match schema").
`rescueFromText` can't repair that, so the tool returns the "unable to
process" error (`internal/agents/callEmbeddedAgent.ts:226`). Production
logs show this exact error with `finishReason: stop` for these queries.
Which of the two shapes the model produced is inferred, since the traces
weren't sampled.

**Change**

- In an absolute `timeRange`, `end` is optional. When it's missing, the
handler runs the search until now.
- `timeRange` defaults to `null`, meaning the default 14d window.
- The prompt has open-ended examples and says "null" instead of "omit".

Existing outputs, whether full `{start, end}` or `{statsPeriod}`, behave
as before.

Related but separate: #1215 / #1228 (caller-supplied `start`/`end` for
`search_events` and `search_issues`) and #2139 / #2146 (dropped absolute
bounds in `runSearchEvents`). Longer term, #1896 removes the embedded
search agents.

<!-- junior-request-attribution:start -->
via @joshuarli.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0CG2CP9ELW%3A1794596896.742009 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CG2CP9ELW%3A1794596896.742009)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CG2CP9ELW%3A1794596896.742009/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
@sentry-junior

sentry-junior Bot commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Another report of this exact case, from Sentry MCP user feedback MCP-SERVER-GQD (python caller, server 0.42.0, search_errors).

Call: query: "Count errors environment:<env> from <ISO start> to <ISO end>", period: "14d". The window was about 7 days, inside the 14d period.

What ran (sampled spans for the call):

  1. The embedded agent's events/validate call used query=environment:<env>&start=<ISO start>&end=<ISO end>. So the agent did return the absolute range.
  2. The final /events/ request used query=environment:<env>&statsPeriod=14d, with no start/end and no timestamp tokens. The search covered the whole 14 days.
  3. The explanation still said the absolute bounds had replaced statsPeriod (that part is Search explanations describe parts the search didn't run #2147).

With this PR, the overlap of 14d and the agent's range is the requested window, so this call would have searched the right window.

The caller's rerun with timestamp:>= / timestamp:< tokens kept them in the final query, so the window was correct there. That path is #2139 and didn't fail this time.

Reports for this case (period + prose window + structured filters): 2 (MCP-SERVER-GN4, MCP-SERVER-GQD). The wider "search drops query parts" pattern is at 12 reports.

@sentry-junior
sentry-junior Bot marked this pull request as ready for review November 15, 2026 06:09
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 15, 2026
@joshuarli
joshuarli merged commit 88c26a3 into main Nov 15, 2026
35 checks passed
@joshuarli
joshuarli deleted the fix/search-events-narrow-period-to-agent-range branch November 15, 2026 07:20
MathurAditya724 added a commit that referenced this pull request Nov 15, 2026
…in (#2277)

Removes the `!` from `return requests[0]!;` in the search-events tests.
#2146 added it on a branch that predates
#2125, which made unnecessary type assertions an error,
so main has rejected it since 88c26a3e.

The return type is still `URLSearchParams`. Nothing else changes.

<!-- junior-request-attribution:start -->
via @MathurAditya724.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:local%3Aweb%3A4c096bdc12f2a2226186d683 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/local%3Aweb%3A4c096bdc12f2a2226186d683)
[[Sentry]](https://sentry.sentry.io/explore/conversations/local%3Aweb%3A4c096bdc12f2a2226186d683/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Aditya Mathur <57684218+MathurAditya724@users.noreply.github.com>

This branch was successfully deployed

1 active and 1 inactive deployments
Actions — 9fa0f74e Deployed Nov 10, 2026 by sentry-junior[bot] via eval #1778
Preview — 9fa0f74e Deployed Nov 10, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant